package com.qk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.ClueDto;
import com.qk.dto.ClueFalseDto;
import com.qk.dto.CluePool;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.ClueService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {

    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;

    @Autowired
    private BusinessMapper businessMapper;


    @Override
    public PageResult<Clue> selectPage(ClueDto clueDto) {
        Page<Clue> page = baseMapper.listPages(new Page<Clue>(clueDto.getPage(),clueDto.getPageSize()),clueDto);
        return new PageResult<>(page.getTotal(),page.getRecords());
    }

    @Override
    public Clue getClueById(Integer id) {
        return baseMapper.getClueById(id);
    }


    @Transactional(rollbackFor = Exception.class) // 开启事务 默认只有运行时异常会回滚，如果要全部异常都捕获，加上属性rollbackFor
    @Override
    public void updateByClueId(Clue clue) {
        //1. 更新线索
        clue.setUpdateTime(LocalDateTime.now());
        clue.setStatus(3);
        baseMapper.updateById(clue);

//        if (true){
//            throw new Exception("测试事务");
//        }

        //2. 新增线索的跟进记录
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(clue.getId());
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        clueTrackRecord.setSubject(clue.getSubject());
        clueTrackRecord.setLevel(clue.getLevel());
        clueTrackRecord.setRecord(clue.getRecord());
        clueTrackRecord.setNextTime(clue.getNextTime());
        clueTrackRecord.setType(1);
        clueTrackRecord.setCreateTime(LocalDateTime.now());


        clueTrackRecordMapper.insert(clueTrackRecord);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateFalseClue(Integer id, ClueFalseDto clueFalseDto) {
        Clue clue = new Clue();
        clue.setId(id);
        clue.setStatus(4);
        baseMapper.updateById(clue);
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(id);
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        clueTrackRecord.setType(0);
        clueTrackRecord.setFalseReason(clueFalseDto.getReason());
        clueTrackRecord.setRecord(clueFalseDto.getRemark());
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void toBusiness(Integer id) {
        //1.转商机
        //1.1查询线索信息
        Clue clue = baseMapper.getClueById(id);
        //1.2修改线索状态
        clue.setStatus(5);
        baseMapper.updateById(clue);
        //1.3新增商机
        Business business = new Business();
        BeanUtils.copyProperties(clue,business);
        business.setId(null);
        business.setStatus(1);
        business.setUserId(null);
        business.setClueId(id);
        business.setNextTime(null);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.insert(business);
    }

    @Override
    public PageResult<Clue> listPage(CluePool cluePool) {
        LambdaQueryWrapper<Clue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(cluePool.getId() != null,Clue::getId, cluePool.getId())
                .like(cluePool.getPhone() != null,Clue::getPhone, cluePool.getPhone())
                .eq(cluePool.getChannel() != null,Clue::getChannel, cluePool.getChannel())
                .eq(Clue::getStatus,4);

        Page<Clue> page = baseMapper.selectPage(new Page<>(cluePool.getPage(), cluePool.getPageSize()), queryWrapper);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }
}
